﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class VGradualPercentageForm : ItemFormTemplate
    {
        public VGradualPercentageForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form_Load);
            this.Shown += new System.EventHandler(this.Form_Shown);
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            view.ViewData = DemoHelper.GetDescription(AppDomain.CurrentDomain.BaseDirectory + "例子\\控件\\GradualPercentageForm\\VGradualPercentageForm.rtf");
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {
            this.introduceBrowser.Rtf = viewObject.ViewData.ToString();
            this.timer.Tick += this.Timer_Tick;

            if (this.ViewVisible)
            {
                this.timer.Start();
            }
        }

        public override void ShowView()
        {
            base.ShowView();

            if (this.ViewStatus > ItemFormTemplateStatus.Create && this.ViewStatus < ItemFormTemplateStatus.Closed)
            {
                this.timer.Start();
            }
        }

        public override void HideView()
        {
            base.HideView();

            if (this.timer != null)
            {
                this.timer.Stop();
            }
        }

        #endregion

        #region 属性面板

        private void Form_Load(object sender, EventArgs e)
        {
            this.controlContainer.SizeChanged += this.ControlContainer_SizeChanged;

            this.demo_control.AnimationEnabled = true;

            this.animationEnabled_toggleSwitch.IsOn = this.demo_control.AnimationEnabled;
            this.animationEnabled_toggleSwitch.Toggled += this.AnimationEnabled_toggleSwitch_Toggled;

        }

        private void Form_Shown(object sender, EventArgs e)
        {
            this.splitContainer.SplitterDistance = Math.Max(this.splitContainer.Width / 4, this.splitContainer.Width - MainForm.GlobalSplitWidth);
            this.splitContainer.SplitterMoved += this.SplitContainer_SplitterMoved;

            MainForm.LoadAssistTabControl(this.assistTabControl);
        }

        private void SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer control = (SplitContainer)sender;
            MainForm.GlobalSplitWidth = this.splitContainer.Width - control.SplitterDistance;
        }

        private void ControlContainer_SizeChanged(object sender, EventArgs e)
        {
            Panel control = (Panel)sender;
            this.demo_control.Location = new Point((control.Width - this.demo_control.Width) / 2, (control.Height - this.demo_control.Height) / 2);
        }

        private void AnimationEnabled_toggleSwitch_Toggled(object sender, EventArgs e)
        {
            ToggleSwitch control = (ToggleSwitch)sender;
            this.demo_control.AnimationEnabled = control.IsOn;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            Random rd = new Random();
            double value = rd.Next(10, 100);
            if (Math.Abs(value - this.demo_control.Value) < 10)
            {
                value = value + Math.Abs(value - this.demo_control.Value) * 10;
            }
            this.demo_control.Value = value;
        }

        #endregion

    }
}
